#!/bin/bash

# 遍历当前目录下的所有子目录
for dir in */; do
    # 进入子目录
    cd "$dir" || continue

    # 检查该目录是否是一个Git仓库
    if [ -d ".git" ]; then
        echo "Processing Git repository in $dir"

        # 获取当前分支名称
        current_branch=$(git symbolic-ref --short -q HEAD)
        echo "Current branch is $current_branch"

        # 获取最新的master分支更新
        git fetch origin master
        if [ $? -ne 0 ]; then
            echo "Failed to fetch master in $dir"
            cd ..
            continue
        fi

        # 合并master到当前分支
        git merge origin/master
        if [ $? -ne 0 ]; then
            echo "Failed to merge master into $current_branch in $dir"
        else
            echo "Successfully merged master into $current_branch in $dir"
        fi
    else
        echo "$dir is not a Git repository"
    fi

    # 返回上一级目录
    cd ..
done

echo "All done!"
